//
//  ClassTest.m
//  MacTestProject
//
//  Created by LiuLichen on 2022/4/11.
//

#import <Foundation/Foundation.h>

#import "ClassTest.h"

#import "Calculator.h"
#import "Calculator+CategoryTest.h"
#import "EOCPerson.h"


NSString* const EOCStringConstant = @"GlobalConstValue";

static const NSString* kConstValue = @"ConstValueWithType";

@implementation ClassTest

- (id) init {
    self = [super init];
    
    {
        self.firstName = @"";
        self.lastName = @"";
        Calculator* c = [[Calculator alloc]init];
        c->pi_ = 3.14;
        NSLog(@"pi = %f", c->pi_);
        double a2 = [c square:2.5];
        NSLog(@"Square of 2.5 is %f", a2);
        double a3 = [c sumOfNum1:2.5 :3.6];
        NSLog(@"Sum of 2.5 and 3.6 is %f", a3);
        [c categoryMethod];
        [c protocolRequiredMethod];
        [c protocolOptionalMethod];

        NSError* error;
        [self doSomeThing:@"YES" : &error];
        NSLog(@"error = %@", error);            // Output: error = (null)
        [self doSomeThing:@"NO" : &error];
        NSLog(@"error = %@", error);            // Output: error = {Domain=xxx, Code=xxx, UserInfo={xxx}}

        NSLog(@"//////////////////////////EOCPerson//////////////////////////");
        NSLog(@"%@", [[EOCPerson alloc]init]);

        {
            NSLog(@"//////////////////////////ZombieTest//////////////////////////");
            // Below codes need to run with the ARC off and the ZombieObjects turned on in  Scheme->Run->Diagnostics.
            /**
             NSObject* obj = [[NSObject alloc] init];
             NSLog(@"Before release obj is : %@", obj);
             [obj release];
             NSLog(@"After release obj is : %@", obj);
             NSString* desc = [obj description];
             NSLog(@"desc = %@", desc);
             */
        }
    }

    return self;
}

- (NSString*) fullName {
    return [NSString stringWithFormat: @"%@ %@", self.firstName, self.lastName];
}

- (void) setFullName: (NSString *)fullName {
    NSArray* components = [fullName componentsSeparatedByString: @" "];
    self.firstName = [components objectAtIndex: 0];
    self.lastName = [components objectAtIndex: 1];
}

- (BOOL) isEqualToClassTest:(id)object {
    if (self == object) {
        return YES;
    }
    ClassTest* other = (ClassTest*)object;
    if (![self.firstName isEqualToString: other.firstName]) {
        return NO;
    }
    if (![self.lastName isEqualToString: other.lastName]) {
        return NO;
    }
    if (self.age != other.age) {
        return NO;
    }
    return YES;
}

- (BOOL) isEqual:(id)object {
    if ([self class] == [object class]) {
        return [self isEqualToClassTest: object];
    } else {
        return [super isEqual: object];
    }
}

- (NSUInteger) hash {
    NSString* strToHash = [NSString stringWithFormat: @"%@:%@:%lu", self.firstName, self.lastName, self.age];
    return [strToHash hash];
}

- (BOOL) doSomeThing:(NSString*)str :(NSError**)error {
    // Do something that may cause an error
    if ([str isEqualToString: @"NO"]) {
        if (error) {
            // Pass the 'error' through the out-parameter
            *error = [NSError errorWithDomain: @"errorDomain" code: 123 userInfo: @{
                @"description": @"some thing wrong",
                @"otherinfo": @"other detail info"
            }];
            return NO;      // Indicate failure
        }
    }
    return YES;         // Indicate success
}

@end
